package com.github.pig.auth.component.mobile;

import com.github.pig.auth.util.LoginType;
import com.github.pig.common.constant.SecurityConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lengleng
 * @date 2018/1/9
 * app登录验证filter
 */
public class MobileAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    public static final String SPRING_SECURITY_FORM_MOBILE_KEY = "mobile";
    public static final String SPRING_SECURITY_FORM_WX_KEY = "openId";
    public static final String SPRING_SECURITY_FORM_QQ_KEY = "qqId";

    private String mobileParameter = SPRING_SECURITY_FORM_MOBILE_KEY;
    private String wxParameter = SPRING_SECURITY_FORM_WX_KEY;
    private String qqParameter = SPRING_SECURITY_FORM_QQ_KEY;

    private boolean postOnly = true;


    public MobileAuthenticationFilter() {
        super(new AntPathRequestMatcher(SecurityConstants.MOBILE_TOKEN_URL, "POST"));
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
                                                HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals(HttpMethod.POST.name())) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());

        }

        String mobile = obtainMobile(request);
        String wx = obtainWX(request);
        if (StringUtils.isNotBlank(wx)){ //微信登录
            MobileAuthenticationToken wxAuthenticationToken = new MobileAuthenticationToken(LoginType.wx.getStatus()+wx);
            setDetails(request, wxAuthenticationToken);
            //setLoginType("_"+wx,LoginType.wx.getStatus()+wx);
            return this.getAuthenticationManager().authenticate(wxAuthenticationToken);
        }else {  //验证码登录

            if (mobile == null) {
                mobile = "";
            }
            mobile = mobile.trim();
            //setLoginType("_"+mobile,LoDefaultTokenServicesginType.mobile.getStatus()+mobile);
            MobileAuthenticationToken mobileAuthenticationToken = new MobileAuthenticationToken(LoginType.mobile.getStatus()+mobile);
            setDetails(request, mobileAuthenticationToken);
            return this.getAuthenticationManager().authenticate(mobileAuthenticationToken);
        }
    }

    protected String obtainMobile(HttpServletRequest request) {
        return request.getParameter(mobileParameter);
    }
    //微信登录
    protected String obtainWX(HttpServletRequest request) {
        return request.getParameter(wxParameter);
    }

    protected void setDetails(HttpServletRequest request,
                              MobileAuthenticationToken authRequest) {
        authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
    }

    /*protected void setLoginType(String type, String value){
        redisTemplate.opsForValue().set(type,value);
    }*/

    public void setPostOnly(boolean postOnly) {
        this.postOnly = postOnly;
    }

    public String getMobileParameter() {
        return mobileParameter;
    }

    public void setMobileParameter(String mobileParameter) {
        this.mobileParameter = mobileParameter;
    }

    public boolean isPostOnly() {
        return postOnly;
    }
}

